package cn.com.text.Activity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.icu.text.LocaleDisplayNames;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import cn.com.text.Adapter.MyPostAdapter;
import cn.com.text.Dao.MyPost;
import cn.com.text.Dao.News;
import cn.com.text.Dao.User;
import cn.com.text.NewsContentActivity;
import cn.com.text.R;
import cn.com.text.Util.ImageHelper;

public class MyPostActivity extends AppCompatActivity {
    private List<MyPost> mypost = new ArrayList<MyPost>();
    private String username = null;
    private ImageView imageView;
    private String imageBit = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_post);
        Intent intent = getIntent();
        username = intent.getStringExtra("userName");
        imageView = (ImageView) findViewById(R.id.imageUserTouxiang);
        List<User> userList = DataSupport.where("name = ?", username).find(User.class);
        if (userList.size() != 0) {
            imageBit = userList.get(0).getImage();
        }
        if (imageBit.equals("")) {
            imageView.setImageResource(R.drawable.touxiang);
        } else {
            imageView.setImageBitmap(ImageHelper.stringToBitmap(imageBit));
        }
        initMyPost();
        MyPostAdapter myPostAdapter = new MyPostAdapter(MyPostActivity.this, R.layout.mypost_item, mypost);
        ListView listview = (ListView) findViewById(R.id.list_MyPost);
        listview.setAdapter(myPostAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                final MyPost post = mypost.get(position);
                showNormalDialog(position);
//                Intent intent = new Intent(MyPostActivity.this,NewsContentActivity.class);
//                intent.putExtra("news_title", post.getNewstitle());
//                intent.putExtra("news_content", post.getNewscontent());
//                startActivity(intent);
            }
        });
    }

    private void initMyPost() {
        mypost = DataSupport.where("username = ?", username).find(MyPost.class);
    }

    private void showNormalDialog(final int position) {
        //创建dialog构造器
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        //设置title
        normalDialog.setTitle("选择");
        //设置内容
        normalDialog.setMessage("你确定这样做么？");
        //设置按钮
        normalDialog.setPositiveButton("查看"
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        final MyPost post = mypost.get(position);
                        Intent intent = new Intent(MyPostActivity.this, NewsContentActivity.class);
                        intent.putExtra("news_title", post.getNewstitle());
                        intent.putExtra("news_content", post.getNewscontent());
                        startActivity(intent);
                        dialog.dismiss();
                    }
                });
        normalDialog.setNegativeButton("更改",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        final MyPost post = mypost.get(position);
                        Intent intent = new Intent(MyPostActivity.this, UpdataPost.class);
                        intent.putExtra("username", username);
                        intent.putExtra("title", post.getNewstitle());
                        intent.putExtra("content", post.getNewscontent());
                        startActivity(intent);
                    }
                });
        //创建并显示
        normalDialog.create().show();
    }
}
